import os
import csv
from structs import *

#Globals#
#dataSetList = os.listdir('resources/datasets/') #List of accessable datasets
#dataSetList.sort()

#dirtyDataSetList = os.listdir('resources/dirtyDataSets/') #List of accessable datasets which have been mucked up by mucker.py
#dirtyDataSetList.sort()



#Functions for Loading Datasets#
def loadDataSet(dataSetName, quiet = 0): #quiet = 0
    if dataSetName in dataSetList or dirtyDataSetList:
        if dataSetName in dirtyDataSetList:
            path = 'resources/dirtyDataSets/'
        if dataSetName in dataSetList:
            path = 'resources/datasets/'
        if quiet == 1:
            print "\nPreparing to Load Chosen DataSet"
            print "--- " + dataSetName +  " is in the resources directory."
            print "--- Loading " + dataSetName + " ..."
        dataSet = loadData(dataSetName, path)
        if quiet == 1:
            print "\nRunning basic tests"
        #runTests(dataSet)
        if quiet == 1:
            print "\nFinished Loading the %s DataSet" % dataSetName
        
    else:
        print "\nError: the %s DataSet does not exist in the resources directory." % dataSetName
    return dataSet

def loadData(dataSetName, path):
    dataSet      = Dataset()
    dataSet.name = dataSetName
    with open (path + dataSetName, 'rb') as csvfile:
        data = csv.reader(csvfile, delimiter = ',')
        rowNum = 0
        for row in data:
            inst = Inst()
            inst.id = rowNum + 1 #This was chosen for ID to correspond to the LINE number in the .csv file
            if rowNum == 0:      #and not the index, this can be easily changed to the index by removing +1
                header = row
                dataSet.features = header
            else:
                column = 0
                for col in row:
                    feat = Feature()
                    feat.id = column
                    feat.header = header[column]
                    if isNum(col) == False:
                        feat.value  = col
                    else:
                        feat.value = float(col)
                    inst.features.append(feat)
                    column += 1
            dataSet.instances.append(inst)
            rowNum += 1
    dataSet.instances.pop(0)
    return dataSet

#Functions for Printing/Writing DataSet#

def printDataSet(dataSet):
    print "DataSet Name: " + dataSet.name
    print "DataSet Feature List: %s" % dataSet.features
    print "DataSet Instances: \n"
    for inst in dataSet.instances:
        print "-----------Instance ID: %s----------" % inst.id
        for feat in inst.features:
            print feat.header + ' ' + feat.value

def printDataSetCSV(dataSetName):
    with open ('resources/datasets/' + dataSetName, 'rb') as csvfile:
        dataSet = csv.reader(csvfile, delimiter = ',')
        rowNum = 0
        for row in dataSet:
            if rowNum == 0:
                header = row
            else:
                column = 0
                for col in row:
                    print '%-8s: %s' % (header[column], col)
                    column += 1
            rowNum += 1
            print "\n"

def writeToCSV(dataSet, numIterations):
    headers = dataSet.features
    writer = csv.writer(open('resources/dirtyDataSets/'+ dataSet.name[:-4] + '-dirty-' + '%s' % numIterations +'.csv', 'wb'))
    writer.writerow(headers)
    for item in dataSet.instances:
        values = []
        for feature in item.features:
            values.append(feature.value)
        zip(values)
        writer.writerow(values)
    
#MISC
def convertDataSet(dataSet):
    #for instance in dataSet.instances:
    #    vals = []
    #    for feat in instance.features[3:]:
    #        vals.append(feat.value)
    print 'DataSet ID        : %s' % dataSet.id
    print 'DataSet Name      : %s' % dataSet.name
    print 'DataSet Feature   : %s' % dataSet.features
    print '--Instances ID    : %s' % dataSet.instances[0].id
    print '----Feature ID    : %s' % dataSet.instances[0].features[15].id
    print '----Feature Header: %s' % dataSet.instances[0].features[15].header
    print '----Feature Value : %s' % dataSet.instances[0].features[15].value

def isNum(string):
    try:
        float(string)
        return True
    except ValueError:
        return False

#Functions Relating to Retreiving Data from DataSets

def dumpFeatVals(dataSet):
    featVals = []
    temp = []
    index = 0
    for feat in dataSet.features:
        vals = []
        for inst in dataSet.instances:
            value = inst.features[index].value
            vals.append(value)
        featVals.append(vals)
        index += 1
    return featVals
        
#TESTS
def runTests(dataSet):
   print "--- No tests to run right now..."
   # printDataSet(dataSet)

